﻿
Partial Class WebForm_Apply
    Inherits System.Web.UI.Page

    Protected Sub AddVolunteer1_Loaded(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Loaded
        Me.AddVolunteer1.InitData()
    End Sub

    Protected Sub AddVolunteer1_Saving(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddVolunteer1.Saving
        If Me.SaveData() = True Then
            Me.MultiView1.SetActiveView(Me.SuccessView)
        End If
    End Sub
End Class

Partial Class WebForm_Apply
    Private Function SaveData() As Boolean
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim i As Integer

        'If Me.AddVolunteer1.VerificationCode <> Me.AddVolunteer1.VerificationCode Then
        '    MyObj.ShowMessage(Me.GetLocalResourceObject("SaveData_InvalidVerificationCode"))
        '    Return False
        'End If

        Using scope As New Transactions.TransactionScope
            '判斷Volunteer是否存在
            sql.Length = 0
            sql.AppendFormat("select v.* ")
            sql.AppendFormat("from VolunteerDistinctView v ")
            sql.AppendFormat("where v.VolunteerID = '{0}' ", Me.AddVolunteer1.VolunteerID)

            If dat.GetData(sql).Rows.Count = 0 Then
                sql.Length = 0
                sql.AppendFormat("insert Volunteer (VolunteerID,VolunteerName,Password,IdentificationID,IdentificationType,Active,RegisterDate,Gender,Birthday,MobilePhone,OfficePhone,BirthAddress,SameAddress,Address,EmergentPerson,EmergentPhone,Email) select ")
                sql.AppendFormat(" '{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerName)
                sql.AppendFormat(",'{0}' ", clsAuth.EncryptPassword(Me.AddVolunteer1.VolunteerPassword))
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.IdentificationType)
                sql.AppendFormat(",'{0}' ", "1")
                sql.AppendFormat(",{0} ", "getdate()")
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Gender)
                sql.AppendFormat(",'{0:yyyy/MM/dd}' ", Me.AddVolunteer1.Birthday)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.MobilePhone)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.OfficePhone)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.BirthAddress)
                sql.AppendFormat(",{0} ", Me.AddVolunteer1.SameAddress)
                If (Me.AddVolunteer1.SameAddress) Then
                    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.BirthAddress)
                Else
                    sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Address)
                End If
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.EmergentPerson)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.EmergentPhone)
                sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.Email)

                dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)
            End If

            sql.Length = 0
            sql.AppendFormat("insert EnlistRecord (EnlistID,VolunteerID,CreateDate,CreateBy,Status) select ")
            sql.AppendFormat(" {0} ", Me.EnlistID)
            sql.AppendFormat(",'{0}' ", Me.AddVolunteer1.VolunteerID)
            sql.AppendFormat(",{0} ", "getdate()")
            sql.AppendFormat(",{0} ", clsAuth.GetUserID())
            sql.AppendFormat(",'{0}' ", My.FormStatus.Approving)
            dat.AccData(sql.ToString(), Nothing, Data.CommandType.Text)

            scope.Complete()
        End Using

        Return True
    End Function

    Private ReadOnly Property EnlistID() As Integer
        Get
            Return Request("EnlistID")
        End Get
    End Property
End Class